﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NetCard.Models;

namespace NetCard.Dao
{
    public class ProjectDao
    {
        private NetCardEntities _entities = new NetCardEntities();

        public List<Project> GetProjects(string userLogin)
        {
            try
            {
                List<Project> projectList = new List<Project>();
                projectList = (List<Project>)(from c in _entities.Project
                               where c.userLogin == userLogin
                               select c);
                return projectList;
            }
            catch
            {
                return null;
            }
        }

        public void InsertProject(Project project)
        {
            try
            {
                _entities.AddToProject(project);
                _entities.SaveChanges();
            }
            catch
            {
            }
        }

        public Project GetProject(long id)
        {
            try
            {
                var project = (from c in _entities.Project
                               where c.id == id
                               select c).FirstOrDefault();
                return (Project)project;
            }
            catch
            {
                return null;
            }
        }

        public void UpdateProject(Project originalProject, Project projectToEdit)
        {
            try
            {
                _entities.ApplyPropertyChanges(originalProject.EntityKey.EntitySetName, projectToEdit);
                _entities.SaveChanges();
            }
            catch
            {
            }
        }

        public void DeleteProject(Project project)
        {
            try
            {
                _entities.DeleteObject(project);
                _entities.SaveChanges();
            }
            catch
            {
            }
        }

    }
}